package com.zretc.controller;


import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.google.gson.Gson;
import com.zretc.entity.OrderInfo;
import com.zretc.entity.RoomInfo;
import com.zretc.entity.UserInfo;
import com.zretc.service.OrderInfoService;

import com.zretc.service.impl.OrderInfoServiceImpl;

import javafx.scene.control.Alert;

/**
 * Servlet implementation class OrderInfoServlet
 */
@WebServlet("/OrderInfoServlet")
public class OrderInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private OrderInfoService orderInfoService = new OrderInfoServiceImpl();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取op参数
		String op = request.getParameter("op");
		//判断op是否是请求渲染页面
		if ("fillRoomMessage".equals(op)) {
			//调用填充房间页面方法
			fillRoomMessage(request,response);
		} else if ("fillInfoMessage".equals(op)) {
			fillInfoMessage(request,response);
		}else if ("sendOrderInfo".equals(op)) {
			sendOrderMessage(request,response);
		}else if ("updateOrderInfo".equals(op)) {
			updateOrderInfoState(request,response);
		}
		
	}
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    private void fillRoomMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿id
		String roomId = request.getParameter("roomId");
		
		//根据民宿id查找该民宿下的所有房间
		List<Map<String, String>> roomMessageByRoomStayId = orderInfoService.getRoomMessageByRoomStayId(roomId);
		//将roomstaylist包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(roomMessageByRoomStayId);
		//将json对象响应给ajax请求
		response.getWriter().write(jsonList);
	}
    private void updateOrderInfoState(HttpServletRequest request, HttpServletResponse response) throws IOException {
  		//获取民宿id
    	HttpSession session = request.getSession();
    	String  orderId = (String)session.getAttribute("orderId");
    	System.out.println("235656866"+orderId);
    	String orderState = "1";
    	Map<String , String> map =new HashMap<String, String>();
    	map.put("orderId", orderId);
    	map.put("orderState", orderState);
    	orderInfoService.getupdateOrderInfoStateByOrderId(map);
    	response.sendRedirect("front/room.jsp");
  	}
    private void sendOrderMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
    	
    	
    	//获取房间Id
    	String roomId = request.getParameter("roomId");
    	//获取用户名
    	String userName = request.getParameter("username");
    	//获取用户手机号
    	String userPhone = request.getParameter("phonenum");
    	//获取房间价格
    	String roomPrice = request.getParameter("roomPrice");
    	String inTime = request.getParameter("inTime");
    	System.out.println(inTime);
    	String outTime = request.getParameter("outTime");
    	System.out.println(outTime);
    	HttpSession session5 = request.getSession();
    	UserInfo user = (UserInfo)request.getSession().getAttribute("user");
    	System.out.println("zzzzzzzzzzzz "+user.getUserId());
    	
    	HttpSession session4 = request.getSession();
    	session4.setAttribute("roomPrice",roomPrice);
    	//通过session获取民宿名
    	HttpSession session = request.getSession();
    	String homeName = (String)session.getAttribute("homestayName");
    	//通过字符串拼接 将orderName放入到session中
    	System.out.println(homeName);
    	StringBuffer stringBuffer = new StringBuffer();
    	stringBuffer.append(homeName)
    				.append(roomId);
    	String orderName = stringBuffer.toString();
    	System.out.println(orderName);
    	HttpSession session1 = request.getSession();
    	session1.setAttribute("orderName",orderName);
    	
    	HttpSession session7 = request.getSession();
    	session7.setAttribute("roomId",roomId);
    	
    	//保留接口   跳转支付界面      往数据库表orderinfo里插入数据  
    	//设置订单状态为0,未支付状态
    	Integer orderState = 0;
    	LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddhhmmss");
		// 将当前日期时间转成字符串
		String formatDate = ofPattern.format(now);	
		
		// 随机数
		int random = (int) (Math.random()*1000+1);
		
		StringBuffer n = new StringBuffer();
		
		n.append(formatDate).append(random);
		
		String orderId = n.toString();
		HttpSession session2 = request.getSession();
    	session2.setAttribute("orderId",orderId);
		System.out.println("llll"+orderId);
		OrderInfo orderInfo = null;
		RoomInfo roomInfo = new RoomInfo(roomId,null, null, null, null);
		UserInfo userInfo = new UserInfo(user.getUserId(), null, null, null, null,null);
    	orderInfo = new OrderInfo(orderId, orderState, inTime, outTime, new UserInfo(user.getUserId(), null, null, null, null,null), new RoomInfo(roomId, null, null, null, null));
    	orderInfoService.InsertOrderInfo(orderInfo);
    	response.sendRedirect("front/payindex.jsp?orderId="+orderId+"&orderPrice="+roomPrice+"");
	}
    private void fillInfoMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿id
		String roomId = request.getParameter("roomId");
		System.out.println(roomId);
		//根据民宿id查找该民宿下的所有房间
		List<Map<String, String>> roomInfoByRoomStayId = orderInfoService.getRoomInfoByRoomStayId(roomId);
		//将roomstaylist包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(roomInfoByRoomStayId);
		//将json对象响应给ajax请求
		response.getWriter().write(jsonList);
	}

}
